Homer dips his toe into some Data Viz! Leaving him here just for Data Viz Spring 2020!
These are my ideas about the grammar of ggplot2. ggplot2 is based on the grammar of graphics. But it has its own grammar too — it is a particular implementation of the grammar of graphics — and a lot of really helpful decisions are part of its grammar (like having set defaults for aesthetic scales, and scale and plot labels make it easier to build plots quickly). If you are teaching ggplot2, you might think about teaching the grammar of ggplot2 first and then come back to the grammar of graphics — because there will be more context for seeing how an implemenation relates to the larger philosophy. There are a lot of great resources out there already teaching the grammar of graphics which are sources of inspiration for the guide.
Not into human language grammar? Just ignor that first column!
| Grammar Analogy | How? | What? | |
|---|---|---|---|
| 1. The Declarative Mood | ggplot(data = gapminder) + | Declaring the data | |
| 2. The Interogative Mood | aes(color = pop) + | Asking for representation of variables by aesthetics (color, size, x position, etc.) (also known as aesthetic mapping) | |
| - Modifiers I | labs(color = continent) + labs(title = “my title”) + |
modifying default aesthetic labels (and plot labels) | |
| - Modifiers II | coord_polar() + | modifying the default coordinate system (how the positional aesthetics appear - x and y) | |
| - Modifiers III | scale_color_viridis_d() + | modifying default aesthetic scales | |
| 3. Nouns | geom_point() + | geometric layers taking on the aesthetics representing variables | |
| 4.The Conditional Mood | geom_point( | Making local, geom specific declarations rather than global declarations | |
| data = gapminder, | data is geom specific |
||
| aes(size = population), | aesthetic representation is geom specific |
||
| color = “blue” |
aesthetics not representing variables; unmapped aesthetics (i.e. The Imperative Mood) | ||
| ) + | |||
| 5. Interjections | annotate(geom = “point”, x = 10, y = 12, color = “blue”) |
Adding context with annotation layers | |
| 6. Punctuation | facet_wrap(~continent) | faceting breaks a plot into small plots (or “small multiples”) based on categorical variables | |
| 7. Greetings | theme_minimal() | themes changing plot look and feel | |
| 8. The Written Language | ggsave(file = “plot.png”, plot = g, height = “4in”, width = “6in”) |
save plots with different resolutions and file formats | |
| 9. Composition | library(patchwork) (g1 + g2) | g3 library(cowplot) plotgrid(…,) |
Composing plots into ensembles | |
| 10. Concision | last_plot(), writing functions | We’ve been intentionally verbose, but we can chose to be more concise |
Above, the geom and theme topics are intentionally cursory. There are tons of geoms, and they can get distracting. Also themes doesn’t directly relate to the data, though it is important. Theme design almost has its own grammar, it can also be put off until fundamentals are taken on.
r viztoc:::build_and_use_gif(path = “scales_files/figure-html/”, pattern = “.”)`
Color
More Annotation topics
External Theme Packages
Spatial
Network
Animation
Extensions:
https://ggforce.data-imaginist.com/ - ggplot extensions gallary - ggforce - ggpattern
ggplot2 is very flexible, so there often there are more than one way of getting things done — which also means there is more than one way to introduce ggplot2. If you aren’t on board with this guide (and even if you are) you may want check out other resouces. The more definitive and comprehensive guide to ggplot2 is the 3rd edition of ggplot2: elegant graphics for data analysis. Some other workshop materials are Alison Hill’s Take a Sad Plot and Make it Better, Garrick Aden-Buie’s A Gentle Guide to the Grammar of Graphics with ggplot2, Will Chase’s R you ready to make charts?, Malcom Barrett’s Designing ggplots making clear figures that communicate, and Thomas Lin Pederson’s Drawing anything with ggplot2. The ggplot2 cheatsheet is also excellent.